<?php

namespace app\adminmini\service;

use app\adminmini\model\Cleaning;
use think\facade\Db;

class CleaningService extends BaseService
{
    public function __construct()
    {
        $this->model = new Cleaning();
    }

    public function getList()
    {

        // 查询条件
        $map = [];


        // 名称
        $name = isset($param['name']) ? trim($param['name']) : '';
        if ($name) {
            $map[] = ['name', 'like', "%{$name}%"];
        }
        $map[] = ['mark', '=', 1];
        $map[] = ['store_id', '=', $this->userId()];

        $list = Db::name('customer_order_cleaning')

            ->where($map)->order('status asc')->order('add_time desc')->page(PAGE, PERPAGE)
            ->select();

        foreach ($list as $i => $info) {
            $course = Db::name('customer_course')->where('id', $info['seat_id'])->find();
            $info['name'] = '';
            if ($course) {
                $info['name'] = $course['name'];
            } else {
                $seat = Db::name('customer_house')->where('id', $info['seat_id'])->find();
                if ($seat) {
                    $info['name'] = $seat['name'];
                } else {
                    if ($info['door_id'] != "") {
                        $info['name'] = "大门门禁";
                    }
                }

            }

            if (!$info['door_id']) {
                if ($info['status'] == 0) {
                    $info['status_name'] = "等待打扫";
                }
                if ($info['status'] == 2) {
                    $info['status_name'] = "已开始";
                }
                if ($info['status'] == 4) {
                    $info['status_name'] = "已完成";
                }
                if ($info['end_time'] < time() && $info['status'] == 0) {
                    $info['status_name'] = "超时未打扫";
                }

                $info['door_id'] = 1;
            } else {
                $info['status_name'] = "大门门禁";
            }


            $list[$i] = $info;
        }
        $count = Db::name('customer_order_cleaning')

            ->where($map)->order('status asc')->page(PAGE, PERPAGE)->count();

        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $message;
    }
}